//
//  SCLoginBindingController.m
//  ScalperMerchant
//
//  Created by 王云 on 2017/10/13.
//  Copyright © 2017年 WY. All rights reserved.
//

#import "SCLoginBindingController.h"
#import "HomeController.h"
#import "IdentitySViewController.h"

@interface SCLoginBindingController ()<UITextFieldDelegate>

@end

@implementation SCLoginBindingController

- (void)viewDidLoad {
    [super viewDidLoad];

     self.title = @"绑定手机号";
      self.view.backgroundColor = [UIColor whiteColor];
      _verButton.layer.borderColor = KOrange.CGColor;
    
    _PromptL.attributedText = [WYNSString Att:@"温馨提示:\n 如待绑定手机号已被其他账号绑定，您将不能绑定该手机号；如您确定需要将当前账号与该手机号做绑定，请返回登录界面，用该手机号登录黄牛派后，并在“我的-设置-绑定手机号”界面，换绑其他手机号，以将该手机号从系统中空出来即可。" rang:@"温馨提示:" font:[UIFont systemFontOfSize:15] color:KOrange ParagraphSpacing:8];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)Verification:(id)sender {
    
    [self.view endEditing:YES];
    __weak __typeof(&*self)weakSelf = self;
    if (![HWCommon isMobileNumber:_phoneF.text]) {
        [SCTool showError:@"请输入正确手机号"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf.phoneF becomeFirstResponder];
        });
        return;
    }
    [self verCode];
}
- (void)verCode{
    
    
    __block NSInteger timeout=59;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    
    [SCApiTool ScSendPhone:_phoneF.text type:@"bind_mobile" success:^(NSString *verStr) {
        [SCTool showSuccess:verStr];
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [_verButton setTitle:@"获取验证码" forState:UIControlStateNormal];
                _verButton.userInteractionEnabled = YES;
                 [_verButton setBackgroundColor:KOrange];
            });
        }else{
            NSInteger seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2ld", (long)seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                [_verButton setTitle:[NSString stringWithFormat:@"%@秒后重发",strTime] forState:UIControlStateNormal];
                _verButton.userInteractionEnabled = NO;
                [_verButton setBackgroundColor:WYColor(215, 215, 215)];
            });
            timeout--;
            
        }
    });
        dispatch_resume(_timer);
        
    } failure:^(NSString *msg) {
        [SCTool showError:msg];
        [_verButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        _verButton.userInteractionEnabled = YES;
        
    }];
    
}



- (IBAction)yesClick:(id)sender {
    [self.view endEditing:YES];
    __weak __typeof(&*self)weakSelf = self;
    if (![HWCommon isMobileNumber:_phoneF.text]) {
        [SCTool showError:@"请输入正确手机号"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf.phoneF becomeFirstResponder];
        });
        return;
    }
    
    if ( _VerF.text.length == 0 ){
        [SCTool showError:@"请输入验证码"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf.VerF becomeFirstResponder];
        });
        
        return;
    }

    [SCApiTool ScBindmobildPhone:_phoneF.text code:_VerF.text                    unioid:_unioid success:^(NSString *msg) {
        [SCTool showSuccess:msg];
        
       
        if ([[SCUserTool loadInfo].joinin_state isEqualToString:@"0"]) {
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                IdentitySViewController *identityVC = [[IdentitySViewController alloc] init];
                [weakSelf.navigationController pushViewController:identityVC animated:YES];

            });
            
        }else{
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [UIApplication sharedApplication].keyWindow.rootViewController =[[JTNavigationController alloc] initWithRootViewController:[[HomeController alloc] init]]; ;
            });
        }

        
        

    } failure:^(NSString *errormsg) {
        
        [SCTool showError:errormsg];
    }];
    
    
}
#pragma mark - textFiled delegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (textField == _phoneF){
        NSInteger existedLength = textField.text.length;
        NSInteger selectedLength = range.length;
        NSInteger replaceLength = string.length;
        if (existedLength - selectedLength + replaceLength > 11) {
            return NO;
        }
        
    }
    
    else if (textField == _VerF){
        NSInteger existedLength = textField.text.length;
        NSInteger selectedLength = range.length;
        NSInteger replaceLength = string.length;
        if (existedLength - selectedLength + replaceLength > 10) {
            return NO;
        }
        
    }
    return YES;
}


- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    
    [_verButton setBackgroundColor:KOrange];
    [_verButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    _verButton.userInteractionEnabled = YES;
}


@end
